﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using NCKH.models;
using NCKH.View.XepLichThi.KhoiTao;
using System.Linq;
using System.Runtime.InteropServices;


namespace NCKH.View.XepLichThi.KhoiTao
{
    public partial class frmThamSoHeThong : DevExpress.XtraEditors.XtraForm
    {
        private string NgayThoiGian { get; set; }
        private string ThoiGianBatDau { get; set; }

        ArrayList CheckEdits, PanelControls, TimeEditsBegin, TimeEditsFinish;
                   
        public frmThamSoHeThong()
        {
            InitializeComponent();
        }

        private void spibuoisang_EditValueChanged(object sender, EventArgs e)
        {
            if (spibuoisang.EditValue.ToString() == "1")
            {
                panelControl1.Visible = true;
                panelControl2.Visible = false;
            }
            else if (spibuoisang.EditValue.ToString() == "2")
            {
                panelControl1.Visible = true;
                panelControl2.Visible = true;
            }
     

            
        }

        private void frmThamSoHeThong_Load(object sender, EventArgs e)
        {
            spibuoisang.Properties.MaxValue = 2;
            spibuoisang.Properties.MinValue = 1;
            spibuoichieu.Properties.MaxValue = 2;
            spibuoichieu.Properties.MinValue = 1;
            CheckEdits = new ArrayList { chkThu2, chkThu3, chkThu4, chkThu5, chkThu6, chkThu7, chkChuNhat };
            PanelControls = new ArrayList 
            { 
                panelControl1, panelControl2, panelControl3, panelControl4, 
            };
            TimeEditsBegin = new ArrayList
            {
                timeEdit1, timeEdit3, timeEdit5, timeEdit7,
            };
            foreach (var timeEdit in TimeEditsBegin)
            {
                (timeEdit as TimeEdit).EditValueChanged += timeEditBegin_EditValueChanged;
            }
            TimeEditsFinish = new ArrayList
            {
                timeEdit2, timeEdit4, timeEdit6, timeEdit8, 
            };
            foreach (var timeEdit in TimeEditsFinish)
            {
                (timeEdit as TimeEdit).EditValueChanged += timeEditFinish_EditValueChanged;
            }
            //(TimeEditsBegin[0] as TimeEdit).EditValue = new TimeSpan(07, 00, 00);
            //(TimeEditsBegin[2] as TimeEdit).EditValue = new TimeSpan(13, 30, 00);
        }
        private void timeEditBegin_EditValueChanged(object sender, EventArgs e)
        {
            var timeEdit = sender as DevExpress.XtraEditors.TimeEdit;
            int index = TimeEditsBegin.IndexOf(timeEdit);
            if (index == -1) return;

            //TimeSpan begin = TimeSpan.Parse((DateTime.Parse(timeEdit.Text)).ToString("HH:mm"));
            (TimeEditsFinish[index] as TimeEdit).EditValue = DateTime.Parse(timeEdit.Text) + (new TimeSpan(0, (int)spiThoiGian.Value, 0));
        }

        private void timeEditFinish_EditValueChanged(object sender, EventArgs e)
        {
            var timeEdit = sender as DevExpress.XtraEditors.TimeEdit;
            int index = TimeEditsFinish.IndexOf(timeEdit);
            if (index == -1) return;

            if (index == 2  - 1 || index == 4 - 1 ) return;

           // TimeSpan finish = TimeSpan.Parse((DateTime.Parse(timeEdit.Text)).ToString("HH:mm"));
            (TimeEditsBegin[index + 1] as TimeEdit).EditValue = DateTime.Parse(timeEdit.Text);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            NgayThoiGian = ThoiGianBatDau = string.Empty;

            for (int i = 0; i < CheckEdits.Count; i++)
            {
                NgayThoiGian += ((CheckEdits[i] as CheckEdit).CheckState == CheckState.Checked) ? "1" : "0";
            }

            for (int i = 0; i < PanelControls.Count; i++)
            {
                if ((PanelControls[i] as PanelControl).Visible == false) continue;

                if (i == 2 || i==4) ThoiGianBatDau += "0000" + ",";

                DateTime time = (DateTime)(TimeEditsBegin[i] as TimeEdit).EditValue;
                ThoiGianBatDau += String.Format("{0:HH}{1:mm},", time, time);
             

            }
            ThoiGianBatDau = ThoiGianBatDau.Substring(0, ThoiGianBatDau.Length - 1);
            Context db = new Context();
            var values = db.PLAN_HocKyDangKy_TCs.Where(a => a.Nam_hoc == frmTaoHocKy.namhoc && a.Hoc_ky == frmTaoHocKy.kyhoc && a.Dot == frmTaoHocKy.dot).Select(b=>b.Ky_dang_ky).ToList();
            var CauHinhThoiGian = new EXM_CauHinhThoiGian()
            {
                Ngay_thoi_gian = NgayThoiGian,
                Thoi_gian_bat_dau = ThoiGianBatDau,
                Ky_dang_ky = values.Single(),
                Kieu_thoi_gian = spiThoiGian.Text,
            };
            db.EXM_CauHinhThoiGians.InsertOnSubmit(CauHinhThoiGian);
            db.SubmitChanges();
            try
            {
                var ThoiGians = new EXM_ThamSoHeThong()
                {
                    Ma_tham_so = "So_ca",
                    Ten_tham_so = "Số ca",
                    ID_ph = 2,
                    Gia_tri = ((decimal)spibuoisang.EditValue + (decimal)spibuoichieu.EditValue).ToString(),
                    Active = true,
                    Ky_dang_ky = values.Single(),
                };
                db.EXM_ThamSoHeThongs.InsertOnSubmit(ThoiGians);
                db.SubmitChanges();
                MessageBox.Show("Lưu thành công");
            }
            catch
            {
               
            }
        }

        private void chkThu7_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void groupControl2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void spibuoichieu_EditValueChanged(object sender, EventArgs e)
        {
            if (spibuoichieu.EditValue.ToString() == "1")
            {
                panelControl3.Visible = true;
                panelControl4.Visible = false;
            }
            else if (spibuoichieu.EditValue.ToString() == "2")
            {
                panelControl3.Visible = true;
                panelControl4.Visible = true;
            }
          
        }
    }
}